package ru.ifmo.tanya.service.impl;

import org.jdom.Element;
import ru.ifmo.tanya.core.huntgroups.HuntGroup;
import ru.ifmo.tanya.core.users.User;
import ru.ifmo.tanya.db.HuntGroupManager;
import ru.ifmo.tanya.keepers.UserStatusKeeper;
import ru.ifmo.tanya.service.XmlService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;


public class ShowHuntGroupsService extends XmlService {

    private HuntGroupManager huntGroupManager = new HuntGroupManager();

    @Override
    public void invoke(User user, HttpServletRequest request, Element response) {
        List<HuntGroup> groups = huntGroupManager.getAllHuntGroups();

        Set<HuntGroup> loggedGroups = new UserStatusKeeper().getGroupsByUser(user);
        Element huntGroups = new Element("hunt-groups");
        for (final HuntGroup group : groups) {
            Element groupElement = group.toXml();
            String status;
            if (loggedGroups != null && loggedGroups.contains(group)) {
                status = "авторизован";
            } else {
                status = "не авторизован";
            }
            groupElement.setAttribute("status", status);
            huntGroups.addContent(groupElement);
        }
        response.addContent(huntGroups);
    }

    @Override
    public String getXslFilePath() {
        return "/xsl/show_hunt_groups.xsl";
    }
}
